from simpy import Environment, RealtimeEnvironment

from models import Goods, Shelf, Instruction

# 货柜规格
size = (10, 10, 1)

shelf_name = '货柜1'


def init_goods() -> list[list[list[Goods]]]:
    """
    数据初始化
    :return:
    """
    goods = list()
    for r in range(size[0]):
        r_g = list()
        for c in range(size[1]):
            c_g = list()
            for i in range(size[2]):
                no = f'{r}{c}{i}'
                i_g = Goods(f'货物-{no}', no, shelf_name, (r, c, i))
                c_g.append(i_g)
            r_g.append(c_g)
        goods.append(r_g)
    return goods


def pick_goods(env: Environment, shelf: Shelf, ins: Instruction):
    """
    取货入口 传入基本参数
    :param env: Environment
    :param shelf: Shelf
    :param ins: Instruction
    :return:
    """
    print(f'[{ins.no}][PickGoods] 计划获取货物 [{env.now}]')
    with shelf.request() as req:
        yield req
        print(f'[{ins.no}][PickGoods] 已获得货柜占用 [{env.now}]')
        item = yield env.process(shelf.get_item(ins))
        print(f'[{ins.no}][PickGoods] 获取到货物 {item} [{env.now}]')


def main():
    # 真实模拟
    env = RealtimeEnvironment(initial_time=0, factor=0.01, strict=False)
    shelf = Shelf(env, "1", "货柜1", pick_pos=(0, 0), transport_pos=0)
    shelf.init_items(init_goods(), 100)
    # 测试指令
    ins_pos_list = [(5, 6, 0), (1, 1, 0), (9, 8, 0), (7, 3, 0)]
    for idx, ins in enumerate(ins_pos_list):
        env.process(pick_goods(env, shelf, Instruction(f'INS-{idx + 1:05d}', ins)))
    #
    env.run()


if __name__ == '__main__':
    main()
